<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends CommonController{


    public function login(){
    	if(IS_POST){
    		$user = D('user');
    		$chk_user = $user -> check(I('username'),I('password'));
    		if(!empty($chk_user)){
    			session('user_id',$chk_user['id']);
    			session('username',$chk_user['username']);
    			$msg['code'] = 1;
				$msg['info'] = '登录成功';
				$msg['status'] = 'success';
				$this->ajaxReturn($msg);
    		}else{
    			$msg['code'] = 0;
				$msg['info'] = '账号密码不匹配';
				$msg['status'] = 'error';
				$this->ajaxReturn($msg);
    		}

    	}else{
    		$this->display();
    	}
    	
    }

    public function register(){
    	if(IS_POST){
    		$info = ['code' => 0 , 'info' => '' , 'status' => 'error'];
    		$data = I('post.');

    		$chk = $this->check_verify($data['verification']);
    			
    		if(!$chk){
    			$info['info'] = '验证码错误';
    			$this->ajaxReturn($info);
    		}else{

    			$user = D('user');

    			if(!$user->create()){
    				$info['info'] = $user->getError();
    				$this->ajaxReturn($info);
    			}else{
    				$register = $user->add();
    				if(!$register){
    					$info['info'] ='注册失败';
    					$this->ajaxReturn($info);
    				}else{
    					$info['code'] = 1;
    					$info['info'] ='注册成功';
    					$info['status'] = 'success';
    					$this->ajaxReturn($info);
    				}

    				
    			}
    			
    		}
    		
    	}else{
    		
    	}

    	$this->display();
    }

    public function logout(){
    	session(null);
		// $this->success();
		// $this->error();
		//重定向
		// $this->redirect('admin/manage/login');
		$this->redirect('login');
    }

    public function verify()
	{

		$config   = array(
			'fontSize'  =>   50,
			'length'    =>   4,
			'codeSet'   => '0123456789',
		);

		$Verify = new \Think\Verify($config);
		$Verify->entry();
	}
	//验证码验证
	public function check_verify($code, $id = '')
	{
	    $verify = new \Think\Verify();

	    return $verify->check($code, $id);
	}
}